﻿/*                              
   Copyright 2011 Nils Kopal, Christopher Krüger, Simone Sauer, Dennis Nolte, Uni Duisburg-Essen

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using Sharebert.LinkLayerLibrary.API;
using Sharebert.LinkLayerLibrary;

namespace Sharebert.OverlayLibrary.API
{
    public interface IOverlay
    {
        /// <summary>
        /// Connects to the p2p network.
        /// </summary>
        void Connect();

        /// <summary>
        /// Perform a clean shutdown, which informs all connected peers.
        /// </summary>
        void Offline();

        /// <summary>
        /// Sends a message through the overlay.
        /// </summary>
        /// <param name="message">The message to send</param>
        void Send(IOverlayMessage message);

        /// <summary>
        /// The used bootstrap client.
        /// </summary>
        IBootstrapClient BootstrapClient { get; }

        /// <summary>
        /// The IP address or hostname of the bootstrap server.
        /// </summary>
        string BootstrapServerAddress { get; }

        /// <summary>
        /// The port number of the bootstrap server.
        /// </summary>
        int BootstrapServerPort { get; }

        /// <summary>
        /// The IP address used to listen for new connections.
        /// </summary>
        string ListenAddress { get; }

        /// <summary>
        /// The port number used to listen for new connections.
        /// </summary>
        int ListenPort { get; }

        /// <summary>
        /// The Peer object of this host.
        /// </summary>
        IPeer LocalPeer { get; }

        /// <summary>
        /// Receiving messages will timeout after the specified time in seconds. 
        /// Peers that do not send us a ping message within this time are assumed to be down.
        /// </summary>
        int ReadTimeout { get; set; }

        /// <summary>
        /// Trying to send messages will timeout after the specified time in seconds.
        /// </summary>
        int WriteTimeout { get; set; }
        
        /// <summary>
        /// Time in seconds, the peer will ping other peers.
        /// </summary>
        int PeerPingInverval { get; set; }

        /// <summary>
        /// Time in seconds, the peer will try to update its finger table
        /// </summary>
        int FingerTableUpdateInterval { get; set; }

        /// <summary>
        /// A list of successor peers. Those peers represent our finger table.
        /// </summary>
        ThreadSafeList<PeerConnection> Successors { get; }

        /// <summary>
        /// A list of predecessor peers. Those peers referencing us in their finger table.
        /// </summary>
        ThreadSafeList<PeerConnection> Predecessors { get; }

        /// <summary>
        /// The bootstrap server is down.
        /// </summary>
        event EventHandler<EventArgs> BootstrapServerIsDown;

        /// <summary>
        /// Received a message from the overlay.
        /// </summary>
        event EventHandler<OverlayMessageReceivedEventArgs> OverlayMessageReceived;

    }
}
